﻿package igz.fleaxy.net.rss 
{
	/**
	 * @author Intelygenz [www.intelygenz.com]
	 * 
	 * Clase ocupada de tratar un RSS (xml)
	 */
	public class RssChannel {
		
		[ArrayElementType("igz.fleaxy.net.rss.RssItem")]
		private var _Items:Array;		
		public function get $Items():Array {
			return _Items;
		}

		public var $Title:String;
		public var $Link:String;
		public var $Description:String;
		public var $Language:String;
		public var $Copyright:String;
		public var $Managingeditor:String;	
		public var $Webmaster:String;	
		public var $Pubdate:String;	
		public var $Lastbuilddate:String;	
		public var $Category:String;	
		public var $Generator:String;	
		public var $Docs:String;	
		public var $Cloud:String;	
		public var $Ttl:String;	
		public var $Image:String;	
		public var $Rating:String;	
		public var $Textinput:String;	
		public var $Skiphours:String;	
		public var $Skipdays:String;
		
		public var $Owner:String;
		public var $Explicit:String;
		
		public var $Updated:String;
		
		
		/**
		 * Recibe un XML y un tipo de rss y lo trata, genera un array de items con todos los elementos. utiliza RSSItem
		 * para generar un item a partir de un subXML con todos los datos
		 * 
		 * @param	p_xml XML con los datos
		 * @param	p_RssType tipo de rss (atom o feed)
		 */
		public function RssChannel( p_xml:XML, p_RssType:int ) {
			_Items = [];
			var fieldName : String;
			var xmlNode : XML;
			for each ( xmlNode in  p_xml.* ) {
//trace( "RssChannel->" + xmlNode.localName() );				
				if ( xmlNode.localName() == "entry" || xmlNode.localName() == "item" ) {
					_Items.push( new RssItem(xmlNode, p_RssType) );
				}
				else {
					fieldName = _GetFieldName( xmlNode.localName().toString() );
					
					if ( this.hasOwnProperty(fieldName) && fieldName != "$Items" ) {
						this[fieldName] = xmlNode.toString();
					}else {
						trace("[WARNING] RssChannel -> El nodo del rss ["+xmlNode.localName().toString()+"] no se puede serializar porque no existe un campo ["+fieldName+"] en RssChannel")
					}
				}
			}
trace( "RssChannel-> Items: " + _Items.length );			
		}
		
		private function _GetFieldName ( p_xmlNodeName:String ) : String {
			var fieldName : String = "$" 
								   + p_xmlNodeName.substr(0, 1).toUpperCase()
								   + p_xmlNodeName.substr(1).toLowerCase()
								   ;
			return fieldName;
		}
	}
}